//
//  ViewController.m
//  15_Work
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#define kCollectionView @"reuse"
#import "LabelCollectionViewCell.h"
#import "LabelCollectionViewLayout.h"
#import <AVFoundation/AVFoundation.h>
#import "SelfSizeTool.h"


@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, LabelCollectionViewLayoutDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray<NSString *> *labelTextArray;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 上边距 (scrollView 自动边距 遇到状态栏 导航栏 自动为页面上的某一个scrollView 所以一般开发中关闭这个属性)
//    self.edgesForExtendedLayout = UIRectEdgeNone; // 和translucent一样的效果
//    self.automaticallyAdjustsScrollViewInsets = NO; // 是viewController的属性
    self.labelTextArray = [NSMutableArray array];
    NSArray *array = @[@"新闻", @"娱乐播报", @"今日焦点", @"音乐", @"好莱坞大电影", @"大事件", @"那些你从没做过的测试"];
    self.labelTextArray = [NSMutableArray arrayWithArray:array];
    [self createCollectionView];
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout height:(CGFloat)height widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    return [SelfSizeTool selfSizeWidthWithString:self.labelTextArray[indexPath.row] height:height font:[UIFont systemFontOfSize:17]];
}

- (void)createCollectionView {
    LabelCollectionViewLayout *layout = [LabelCollectionViewLayout new];
    layout.delegate = self;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 40) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor cyanColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"LabelCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectionView];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.labelTextArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionView forIndexPath:indexPath];
    cell.backgroundColor = [UIColor yellowColor];
    cell.label.text = self.labelTextArray[indexPath.row];
    return cell;

}















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
